/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuscarPartidoElectoral.java
 *
 * Created on 14-ene-2013, 6:43:32
 */
package sisvoto.vista;

import java.io.IOException;
import java.util.ArrayList;
import sisvoto.DAO.ConsultasBD;
import sisvoto.modelo.PartidoElectoral;
import sisvoto.modeloTablas.ModelTablePartido;

/**
 *
 * @author DJ2-PC
 */
public class BuscarPartidoElectoral extends javax.swing.JDialog {

    ModelTablePartido modelo;
    PartidoElectoral partido;
    ArrayList<PartidoElectoral> partidos;
    AdministrarPartidoElectoral admpartido = null;
    AdministrarElecciones adminelec = null;

    /** Creates new form BuscarPartidoElectoral */
    public BuscarPartidoElectoral(java.awt.Frame parent, boolean modal, AdministrarPartidoElectoral admpartido) {
        super(parent, modal);
        this.admpartido = admpartido;
        initComponents();
        actualizartabla((ArrayList<PartidoElectoral>) new ConsultasBD().buscarTodos("PartidoElectoral"));
    }

    public BuscarPartidoElectoral(java.awt.Frame parent, boolean modal, AdministrarElecciones adminelec) {
        super(parent, modal);
        this.adminelec = adminelec;
        initComponents();
        actualizartabla((ArrayList<PartidoElectoral>) new ConsultasBD().buscarTodos("PartidoElectoral"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblBusPartElec = new javax.swing.JLabel();
        jLblCrtBus = new javax.swing.JLabel();
        jCbxPartidoElectoral = new javax.swing.JComboBox();
        txtBuscar = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTblPartidoElectoral = new javax.swing.JTable();
        jBtnImportar = new javax.swing.JButton();
        jBtnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Partidos Electorales");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/''_10102012_1738.jpeg"))); // NOI18N

        curvesPanel1.setLayout(null);

        jLblBusPartElec.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblBusPartElec.setForeground(new java.awt.Color(255, 255, 255));
        jLblBusPartElec.setText("Partidos Electorales");
        curvesPanel1.add(jLblBusPartElec);
        jLblBusPartElec.setBounds(160, 30, 250, 21);

        jLblCrtBus.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblCrtBus.setForeground(new java.awt.Color(255, 255, 255));
        jLblCrtBus.setText("Criterio de Búsqueda:");
        curvesPanel1.add(jLblCrtBus);
        jLblCrtBus.setBounds(30, 70, 160, 30);

        jCbxPartidoElectoral.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nombre", "Alias", "Código", "Todos" }));
        curvesPanel1.add(jCbxPartidoElectoral);
        jCbxPartidoElectoral.setBounds(200, 70, 230, 30);
        curvesPanel1.add(txtBuscar);
        txtBuscar.setBounds(30, 110, 270, 40);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/search_32.png"))); // NOI18N
        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        curvesPanel1.add(jButton1);
        jButton1.setBounds(311, 110, 120, 40);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Partidos Electorales", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 10), java.awt.Color.white)); // NOI18N
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setOpaque(false);
        jPanel1.setLayout(null);

        jTblPartidoElectoral.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTblPartidoElectoral.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTblPartidoElectoralMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTblPartidoElectoral);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(10, 20, 380, 130);

        curvesPanel1.add(jPanel1);
        jPanel1.setBounds(30, 160, 400, 160);

        jBtnImportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/right_32.png"))); // NOI18N
        jBtnImportar.setText("Importar");
        jBtnImportar.setEnabled(false);
        jBtnImportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnImportarActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnImportar);
        jBtnImportar.setBounds(130, 330, 110, 41);

        jBtnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/DeleteRed.png"))); // NOI18N
        jBtnSalir.setText("Salir");
        jBtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSalirActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnSalir);
        jBtnSalir.setBounds(250, 330, 110, 41);

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    jBtnImportar.setEnabled(false);
    if (jCbxPartidoElectoral.getSelectedItem().toString().equals("Código")) {
        this.partidos = (ArrayList<PartidoElectoral>) new ConsultasBD().buscarPOR("PartidoElectoral", "id", "'" + txtBuscar.getText() + "'");
        actualizartabla(partidos);
    } else {
        if (jCbxPartidoElectoral.getSelectedItem().toString().equals("Todos")) {
            this.partidos = (ArrayList<PartidoElectoral>) new ConsultasBD().buscarTodos("PartidoElectoral");
        } else {
            if (jCbxPartidoElectoral.getSelectedItem().toString().equals("Nombre")) {
                this.partidos = (ArrayList<PartidoElectoral>) new ConsultasBD().buscarLIKE("PartidoElectoral", "nombre", txtBuscar.getText());
            } else {
                this.partidos = (ArrayList<PartidoElectoral>) new ConsultasBD().buscarLIKE("PartidoElectoral", "aliasp", txtBuscar.getText());
            }
        }
        actualizartabla(partidos);
    }

}//GEN-LAST:event_jButton1ActionPerformed

private void jTblPartidoElectoralMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTblPartidoElectoralMouseClicked
// TODO add your handling code here:
    if (jTblPartidoElectoral.getSelectedRow() != -1) {
        Integer idpriv = (Integer) jTblPartidoElectoral.getValueAt(jTblPartidoElectoral.getSelectedRow(), 0);
        ArrayList<PartidoElectoral> paux = (ArrayList<PartidoElectoral>) new ConsultasBD().buscarPOR("PartidoElectoral", "id", "'" + idpriv + "'");
        this.partido = paux.get(0);
        jBtnImportar.setEnabled(true);
    }
}//GEN-LAST:event_jTblPartidoElectoralMouseClicked

private void jBtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSalirActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_jBtnSalirActionPerformed

private void jBtnImportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnImportarActionPerformed
// TODO add your handling code here:
    AdministrarPartidoElectoral ap;
    if (admpartido != null) {
        try {
            this.dispose();
            admpartido.dispose();
            ap = new AdministrarPartidoElectoral(null, true, partido);
            ap.setLocationRelativeTo(this);
            ap.setVisible(true);
        } catch (IOException ex) {
        }
    }
    if(adminelec!=null){
        adminelec.listas.add(partido);
        this.dispose();
    }

}//GEN-LAST:event_jBtnImportarActionPerformed

    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        
        //</editor-fold>

        /* Create and display the dialog */
        /*java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                BuscarPartidoElectoral dialog = new BuscarPartidoElectoral(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JButton jBtnImportar;
    private javax.swing.JButton jBtnSalir;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCbxPartidoElectoral;
    private javax.swing.JLabel jLblBusPartElec;
    private javax.swing.JLabel jLblCrtBus;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTblPartidoElectoral;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables

    private void actualizartabla(ArrayList<PartidoElectoral> partidos) {
        this.partidos = partidos;
        modelo = new ModelTablePartido(partidos);
        jTblPartidoElectoral.setModel(modelo);
        jTblPartidoElectoral.updateUI();
    }
}
